package by.kniazhych.core.server.local.logic.peaces {
	import by.kniazhych.core.data.game.PeaceData;
	import by.kniazhych.core.data.game.PeacePosition;
	import by.kniazhych.core.server.local.logic.LogicController;
	import by.vaukalak.core.math.VMath;
	import flash.geom.Point;
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	public class GetmanPeace extends AbstractPeace{
		
		public function GetmanPeace(controller:LogicController, color:uint) {
			super(controller, color);
		}
		
		override public function checkMotion(sP:PeacePosition, fP:PeacePosition):Boolean {
			return this._checkHorizontal(sP,fP) || this._checkTraverse(sP, fP);
		}
		
		protected function _checkHorizontal(sP:PeacePosition, fP:PeacePosition):Boolean {
			var dY:int = fP.y - sP.y;
			var dX:int = fP.x - sP.x;
			if (dY != 0 && dX != 0) return false;
			if (super._isMyCell(fP)) return false;
			if (dY != 0) {
				for (var i:int = VMath.getSign(dY); i != dY; i += VMath.getSign(dY)) {
					var yPos:Number = sP.y + i;
					var xP:PeacePosition = new PeacePosition(sP.x, yPos);
					if (!super._checkCellClear(xP) || _controllTronIllegalTraverse(xP, fP)) return false;
				}
			}
			//if (sP.x == 5 && sP.y == 4 && fP.x == 4 && sP.x == 4) trace("fuck");
			if (dX != 0) {
				for (var j:int = VMath.getSign(dX); j != dX; j += VMath.getSign(dX)) {
					var xPos:Number = sP.x + j;
					var yP:PeacePosition = new PeacePosition(xPos, sP.y);
					if (!super._checkCellClear(yP) || _controllTronIllegalTraverse(yP, fP)) return false;
				}
			}
			return true;
		}
		
		protected function _controllTronIllegalTraverse(p:PeacePosition, fP:PeacePosition):Boolean {
			return (fP.x != 4 || fP.y != 4) && (p.x == 4 && p.y == 4);
		}
		
		protected function _checkTraverse(sP:PeacePosition, fP:PeacePosition):Boolean {
			var dY:int = fP.y - sP.y;
			var dX:int = fP.x - sP.x;
			if (Math.abs(dY) != Math.abs(dX)) return false;
			if (super._isMyCell(fP)) return false;
			for (var i:int = VMath.getSign(dY); i != dY; i += VMath.getSign(dY)) {
				var xPos:Number = sP.x + VMath.getSign(dX) * Math.abs(i);
				var yPos:Number = sP.y + i;
				var p:PeacePosition = new PeacePosition(xPos, yPos);
				if (!super._checkCellClear(p) || _controllTronIllegalTraverse(p, fP)) return false;
			}
			return true;
		}
		
	}

}